package com.a1anwang.okble.demo.views;

import com.a1anwang.okble.common.OKBLEDataUtils;
import com.a1anwang.okble.demo.ResourceTable;
import com.a1anwang.okble.demo.utils.TextUtilsEx;

import com.a1anwang.okble.demo.utils.ToastUtil;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.UUID;

public class ServiceDataInputPopupWindow  implements Component.ClickedListener {
    boolean inUuid = true;
    private TextField tvUuid;
    private TextField tvData;
    private Button btnRandom;
    private Component layoutUuid;
    private Component layoutData;
    private CommonDialog popWindow = null;
    private Context mContext;
    private InputListener inputListener;

    public void setInputListener(InputListener inputListener) {
        this.inputListener = inputListener;
    }
    /**
     * 构造函数
     *
     * @param context 上下文
     */

    public ServiceDataInputPopupWindow(Context context) {
        this.mContext = context;
        int screenWidht = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes().width;
        int screenHeight = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes().height;
        popWindow = new CommonDialog(mContext);
        Component layout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_service_data_input, null, true);
        popWindow.setAlignment(TableLayout.Alignment.ALIGNMENT_BOTTOM);
        popWindow.setTransparent(true);
        popWindow.setContentCustomComponent(layout);
        popWindow.setSize(screenWidht,screenHeight);
        init(layout);
    }

    private void init(Component layout) {
        tvUuid = (TextField) layout.findComponentById(ResourceTable.Id_tv_uuid);
        tvData = (TextField) layout.findComponentById(ResourceTable.Id_tv_data);
        tvUuid.setClickedListener(this);
        tvData.setClickedListener(this);
        layoutUuid = layout.findComponentById(ResourceTable.Id_layout_uuid);
        layoutData = layout.findComponentById(ResourceTable.Id_layout_data);
        layoutUuid.setClickedListener(this);
        layoutData.setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_0).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_1).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_2).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_3).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_4).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_5).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_6).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_7).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_8).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_9).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_a).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_b).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_c).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_d).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_e).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_f).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_x).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_done).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_back).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_line).setClickedListener(this);
        btnRandom = (Button) layout.findComponentById(ResourceTable.Id_btn_random);
        btnRandom.setClickedListener(this);
    }
    /**
     * 展示 PopWindow
     */
    public void showAt() {
        popWindow.show();
    }
    /**
     * 隐藏 PopWindow
     */
    public void dismiss() {
        if (popWindow != null) {
            popWindow.destroy();
        }
    }
    /**
     * onClick处理点击事件
     *
     * @param component 控件对象
     */
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_layout_uuid:
                inUuid = true;
                btnRandom.setVisibility(Component.VISIBLE);
                break;
            case ResourceTable.Id_layout_data:
                inUuid = false;
                btnRandom.setVisibility(Component.HIDE);
                break;
            case ResourceTable.Id_btn_0:
                if (inUuid) {
                    tvUuid.append("0");
                } else {
                    tvData.append("0");
                }
                break;
            case ResourceTable.Id_btn_1:
                if (inUuid) {
                    tvUuid.append("1");
                } else {
                    tvData.append("1");
                }
                break;
            case ResourceTable.Id_btn_2:
                if (inUuid) {
                    tvUuid.append("2");
                } else {
                    tvData.append("2");
                }
                break;
            case ResourceTable.Id_btn_3:
                if (inUuid) {
                    tvUuid.append("3");
                } else {
                    tvData.append("3");
                }
                break;
            case ResourceTable.Id_btn_4:
                if (inUuid) {
                    tvUuid.append("4");
                } else {
                    tvData.append("4");
                }
                break;
            case ResourceTable.Id_btn_5:
                if (inUuid) {
                    tvUuid.append("5");
                } else {
                    tvData.append("5");
                }
                break;
            case ResourceTable.Id_btn_6:
                if (inUuid) {
                    tvUuid.append("6");
                } else {
                    tvData.append("6");
                }
                break;
            case ResourceTable.Id_btn_7:
                if (inUuid) {
                    tvUuid.append("7");
                } else {
                    tvData.append("7");
                }
                break;
            case ResourceTable.Id_btn_8:
                if (inUuid) {
                    tvUuid.append("8");
                } else {
                    tvData.append("8");
                }
                break;
            case ResourceTable.Id_btn_9:
                if (inUuid) {
                    tvUuid.append("9");
                } else {
                    tvData.append("9");
                }
                break;
            case ResourceTable.Id_btn_a:
                if (inUuid) {
                    tvUuid.append("A");
                } else {
                    tvData.append("A");
                }
                break;
            case ResourceTable.Id_btn_b:
                if (inUuid) {
                    tvUuid.append("B");
                } else {
                    tvData.append("B");
                }
                break;
            case ResourceTable.Id_btn_c:
                if (inUuid) {
                    tvUuid.append("C");
                } else {
                    tvData.append("C");
                }
                break;
            case ResourceTable.Id_btn_d:
                if (inUuid) {
                    tvUuid.append("D");
                } else {
                    tvData.append("D");
                }
                break;
            case ResourceTable.Id_btn_e:
                if (inUuid) {
                    tvUuid.append("E");
                } else {
                    tvData.append("E");
                }
                break;
            case ResourceTable.Id_btn_f:
                if (inUuid) {
                    tvUuid.append("F");
                } else {
                    tvData.append("F");
                }
                break;
            case ResourceTable.Id_btn_line:
                if (inUuid) {
                    tvUuid.append("-");
                } else {
                    tvData.append("-");
                }
                break;
            case ResourceTable.Id_btn_x:
                String nowStr;
                if (inUuid) {
                    nowStr = tvUuid.getText().toString();
                    if (nowStr.length() > 0) {
                        tvUuid.setText(nowStr.substring(0,nowStr.length() - 1));
                    }
                } else {
                    nowStr = tvData.getText().toString();
                    if (nowStr.length() > 0) {
                        tvData.setText(nowStr.substring(0,nowStr.length() - 1));
                    }
                }
                break;
            case ResourceTable.Id_btn_done:
                String uuid = tvUuid.getText().toString();
                if (!OKBLEDataUtils.isValidShortUUID(uuid) && !OKBLEDataUtils.isValidUUID(uuid)) {
                    ToastUtil.toast(mContext,"uuid is not valid");
                    return;
                }
                String value = tvData.getText().toString();
                if (TextUtilsEx.isEmpty(value)) {
                    ToastUtil.toast(mContext,"data is not valid");
                    return;
                }
                if (inputListener != null) {
                    inputListener.onInputComplete(uuid,value);
                }
                tvUuid.setText("");
                tvData.setText("");
                popWindow.destroy();
                break;
            case ResourceTable.Id_btn_back:
                tvUuid.setText("");
                tvData.setText("");
                popWindow.destroy();
                break;
            case ResourceTable.Id_btn_random:
                tvUuid.setText(UUID.randomUUID().toString().toUpperCase());
                break;
            default:
                break;
        }
    }

    /**
     * InputListener 接口
     *
     * @since 2021-05-14
     */

    public interface InputListener {
        /**
         * put函数
         *
         * @param uuid uuid
         * @param value value
         *
         */
        void onInputComplete(String uuid,String value);
    }
}
